package org.eclipse.jdt.internal.ui.callhierarchy;

import java.util.HashMap;
import java.util.Map;

/**
 * 模板引擎
 * .
 * @author hehuang
 *
 */
public class TemplateEngine {
	
	private Map<TemplateFlag, Template> registry = new HashMap<TemplateFlag, Template>();
	
	public TemplateEngine() {
		AutoLoadTemplate auto = new AutoLoadTemplate();
		registry.put(auto.getTemplateFlag(), auto);
		
		ValueNameTemplate valueName = new ValueNameTemplate();
		registry.put(valueName.getTemplateFlag(), valueName);
		
		CallSuperTemplate callSuper = new CallSuperTemplate();
		registry.put(callSuper.getTemplateFlag(), callSuper);
		
		GetterMethodTemplate getterMethodTemplate = new GetterMethodTemplate();
		registry.put(getterMethodTemplate.getTemplateFlag(), getterMethodTemplate);
		
		GetterMethodWithBodyTemplate getterMethodWithBodyTemplate = new GetterMethodWithBodyTemplate();
		registry.put(getterMethodWithBodyTemplate.getTemplateFlag(), getterMethodWithBodyTemplate);
		
		MethodDeclarationTemplate methodDeclarationTemplate = new MethodDeclarationTemplate();
		registry.put(methodDeclarationTemplate.getTemplateFlag(), methodDeclarationTemplate);
		
		MethodMethodWithBodyTemplate methodMethodWithBodyTemplate = new MethodMethodWithBodyTemplate();
		registry.put(methodMethodWithBodyTemplate.getTemplateFlag(), methodMethodWithBodyTemplate);
		
		SetterMethodTemplate setterMethodTemplate = new SetterMethodTemplate();
		registry.put(setterMethodTemplate.getTemplateFlag(), setterMethodTemplate);
		
		ValueNameMethodTemplate valueNameMethodTemplate = new ValueNameMethodTemplate();
		registry.put(valueNameMethodTemplate.getTemplateFlag(), valueNameMethodTemplate);
		
		FieldGetterTemplate fieldGetterTemplate = new FieldGetterTemplate();
		registry.put(fieldGetterTemplate.getTemplateFlag(), fieldGetterTemplate);
		
		FieldGetterMethodWithBodyTemplate fieldGetterMethodWithBodyTemplate = new FieldGetterMethodWithBodyTemplate();
		registry.put(fieldGetterMethodWithBodyTemplate.getTemplateFlag(), fieldGetterMethodWithBodyTemplate);
	}

	public String build(TemplateFlag flag, Map<String, String> params) {
		Template template = registry.get(flag);
		
		if (template != null
				&& params != null
				&& params.size() > 0) {
			String content = template.createTemplateContent();
			
			// 模板处理
			// 1. 支持@param占位
			// 2. 支持@@param 首字母大写,用于getter和setter方法名的生成
			for (Map.Entry<String, String> item
					: params.entrySet()) {
				String key =  item.getKey();
				String value = item.getValue();
				
				if (key != null
						&& key.startsWith("@")) { // 判断是否为占位符
					try {
						// 先处理@@param
						String camelKey = "@" + key;
						if (value != null
								&& value.length() > 0) {
							String camelValue = value.substring(0, 1).toUpperCase() + value.substring(1);
							content = content.replaceAll(camelKey, camelValue);
						}
					} catch (Exception e) {
						Logger.info("引擎处理失败" + e.getMessage());
					}
					
					content = content.replaceAll(key, value);
				}
			}
			
			return content;
		}
		
		return "";
	}
}
